package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.dbtools.AjaxResponse;
import com.zretc.emptraining.entity.Dept;
import com.zretc.emptraining.service.DeptService;
import com.zretc.emptraining.service.EmpService;
import com.zretc.emptraining.serviceimpl.DeptServiceImpl;
import com.zretc.emptraining.serviceimpl.EmpServiceImpl;

/**
 * Servlet implementation class DeptServlet
 */
@WebServlet("/DeptServlet")
public class DeptServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	DeptService dsi = new DeptServiceImpl();
	EmpService es = new EmpServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeptServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String op = "";
		if(request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if("".equals(op)) {
			showAll(request, response);
		}else if("add".equals(op)) {
			addDept(request, response);
		}else if("del".equals(op)) {
			delDept(request, response);
		}else if("upd".equals(op)) {
			updDept(request, response);
		}else if("delAll".equals(op)) {
			delAll(request, response);
		}else if("checkDeptname".equals(op)) {
			checkName(request, response);
		}else if("ajaxDept".equals(op)) {
			ajaxDept(response);
		}else if("ajaxManager".equals(op)) {
			ajaxManager(request, response);
		}
	}

	private void ajaxManager(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 判断要修改的主管是否已经是其它部门的主管
		response.setContentType("application/json;charset=utf-8");
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		String msg = "";
		int code = 200;
		List<Dept> list = dsi.getAllDept("");
		for (int i = 0; i < list.size(); i++) {
			int n = list.get(i).getEmpNo();
			if(n == empNo) {
				msg = "该员工已经是其它部门的主管";
				code = 0;
			}
		}
		Gson(msg , code , response);
	}

	private void ajaxDept(HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		List<Dept> list = dsi.getAllDept("");
		System.out.println(list);
		Gson gson = new Gson();
		String ListDept = gson.toJson(list);
		// 4 响应
		PrintWriter out = response.getWriter();
		out.print(ListDept);
		// 5.关闭
		out.close();
	}

	private void checkName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String deptname = request.getParameter("deptname");
		boolean flag = dsi.existDeptName(deptname);
		String msg = "部门名已存在";
		int code = 0;
		if(flag) {
			if(!deptname.equals("")) {
				msg = "部门名可用";
				code = 200;
			}
		}
		Gson(msg , code , response);
	}

	private void delAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String s = request.getParameter("r");
		s = s.substring(0,s.length()-1);
		String[] strs = s.split(",");
		
		boolean flag = false;
		String msg = "删除失败!";
		int code = 0;
		for(int i = 0;i< strs.length;i++) {
			flag = dsi.delDept(Integer.parseInt(strs[i]));
		}
		if(flag) {
			msg = "删除成功!";
			code = 200;
		}
		Gson(msg , code , response);
	}

	private void updDept(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int deptNo = Integer.parseInt(request.getParameter("deptno"));
		String deptName = request.getParameter("deptname");
		int empNo = Integer.parseInt(request.getParameter("empno"));
		Dept dept = new Dept(deptNo, deptName, empNo);
		String msg = "修改失败";
		int code = 0;
		boolean flag = dsi.updDept(dept);
		boolean f = es.updEmpDeptJob(empNo, deptNo, "主管");
		if(flag) {
			if(f) {
				msg = "修改成功";
				code = 200;
			}
		}
		Gson(msg , code , response);
	}

	private void delDept(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int deptno = Integer.parseInt(request.getParameter("deptno"));
		boolean flag = dsi.delDept(deptno);
		String msg = "删除失败";
		int code = 0;
		if(flag) {
			msg = "删除成功";
			code = 200;
		}
		Gson(msg , code , response);
	}

	private void addDept(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String deptName = request.getParameter("deptname");
		String msg = "添加失败!";
		int code = 0;
		if(request.getParameter("empno").equals("暂无主管")) {
			Dept dept = new Dept(0, deptName, 0);
			boolean flag = dsi.addDeptNoManager(dept);
			if(flag) {
				msg = "添加成功";
				code = 200;
			}
			Gson(msg, code ,response);
		}else {
			int empNo = Integer.parseInt(request.getParameter("empno"));
			Dept dept = new Dept(0, deptName, empNo); 
			boolean f1 = dsi.addDept(dept);
			if(f1) {
				int deptNo = dsi.getDeptnoByDeptname(deptName);
				boolean f2 = es.updEmpDeptJob(empNo, deptNo, "主管");
				if(f2) {
					msg = "添加成功!";
					code = 200;
				}
			}
			Gson(msg, code ,response);
		}
	}

	private void showAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int page = 1;
		if(request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		int pageSize = 10 ;
		if(request.getParameter("pageSize") != null) {
			page = Integer.parseInt(request.getParameter("pageSize"));
		}
		String keyword = "";
		if(request.getParameter("keyword") != null) {
			keyword = request.getParameter("keyword");
		}
		
		PageData<Dept> pd = dsi.getPageByDeptName(page, pageSize, keyword);
		request.setAttribute("pd", pd);
		
		request.setAttribute("keyword", keyword);
		request.getRequestDispatcher("DeptList.jsp").forward(request, response);
	}
	
	public void Gson(String msg , int code ,HttpServletResponse response) throws IOException {
		Gson gson = new Gson();
		AjaxResponse ajaxres = new AjaxResponse(msg, code);
		String jsonString = gson.toJson(ajaxres);
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.print(jsonString);
		outPrintWriter.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
